package com.xbmzdx.fstj_app.controller;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.xbmzdx.fstj_app.dao.UsertableMapper;
import com.xbmzdx.fstj_app.pojo.Brandtable;
import com.xbmzdx.fstj_app.pojo.Usertable;
import com.xbmzdx.fstj_app.service.photocheck.CheckPhotoService;
import com.xbmzdx.fstj_app.service.photocheck.uploadFileToOss;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljq
 * @since 2022-03-13
 */
@RestController
@RequestMapping("/usertable")
public class UsertableController {

    @Autowired
    public UsertableMapper usertableMapper;
    @Autowired
    public CheckPhotoService checkPhotoService;

    @ApiOperation(value = "注册")
    @PostMapping("/add")
    public String add( @RequestBody Usertable user){
        int i;
       QueryWrapper queryWrapper = new QueryWrapper();
       queryWrapper.eq("Phone",user.getPhone());
        Usertable usertable = usertableMapper.selectOne(queryWrapper);
        if (usertable==null) {
            user.setIsdelete(0);
            user.setVersion(0);
            String encode = MD5.create().digestHex(user.getUserPassword(), StandardCharsets.UTF_8);
            user.setUserPassword(encode);
            i = usertableMapper.insert(user);
        }else {
            usertable.setIsdelete(0);
            i = usertableMapper.updateById(user);
        }
        if (i == 1) {
            return "注册成功";
        }
        return "注册失败";
    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Usertable login( @RequestParam String phone,@RequestParam String password){
        String encode = MD5.create().digestHex(password,StandardCharsets.UTF_8);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Phone",phone);
        queryWrapper.eq("UserPassword",encode);
        Usertable usertable = usertableMapper.selectOne(queryWrapper);
        return usertable;
    }


    @ApiOperation(value = "查询所有用户")
    @PostMapping("/selectUser")
    public List selectUser(){
        List<Usertable> users = usertableMapper.selectList (null);
        return users;
    }

    @ApiOperation(value = "查询单个用户")
    @PostMapping("/selectOne")
    public Usertable selectById(String id){
        Usertable usertable = usertableMapper.selectById(id);
        return usertable;
    }


    @ApiOperation(value = "注销用户")
    @PostMapping("/deleteUser")
    public String deleteUser(String phone){
        QueryWrapper<Usertable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        Usertable usertable = usertableMapper.selectOne(queryWrapper);
        usertable.setIsdelete(1);
        int update = usertableMapper.update(usertable, queryWrapper);
        if (update==1){
            return "注销成功";
        }
        return "注销失败";
    }


    @ApiOperation(value = "修改用户信息")
    @PostMapping("/updateUser")
    public String updateUser(@RequestBody Usertable user){
        String encode = MD5.create().digestHex(user.getUserPassword(),StandardCharsets.UTF_8);
        user.setVersion(user.getVersion()+1);
        user.setUserPassword(encode);
        int i = usertableMapper.updateById(user);
        if (i==1){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    @ApiOperation(value = "上传图片")
    @PostMapping(value = "/fileUpload")
    public String uploadPhoto(@RequestPart(value = "file") MultipartFile file) throws IOException {
        String path = checkPhotoService.fileUpload (file);
        FileInputStream fileInputStream = new FileInputStream(path);
        return "https://test11268.oss-cn-beijing.aliyuncs.com/"+ uploadFileToOss.uploadFileToOss ("oss-cn-beijing.aliyuncs.com","LTAI5tFjWmQHDfZEuGXKebbL","9EwvdfVNsQ8b1vFgLtE6bGLos0kxaU",fileInputStream,
                "test11268","",file.getOriginalFilename ());
    }


}
